//
//  Down_View.m
//  MZJ
//
//  Created by XDD on 16/6/23.
//  Copyright © 2016年 XDD. All rights reserved.
//

#import "Down_View.h"

@interface Down_View () {
    
    NSMutableArray *nameArr;
    UIView *view;
    
//    NSArray *cityArr;
    NSInteger page;
//    NSMutableArray *cityArrays;
    NSArray *btnTitles;
    UIButton *aBt;
    UIView *view1;
}

@end

@implementation Down_View

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        
        [self initializeView];
    }
    return self;
}

- (void)initializeView {
    _mBview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight - 44)];
    _mBview.backgroundColor = [self colorWithHexString:@"000000" withAlpha:0.75];
    [self addSubview:_mBview];
    
    nameArr = [NSMutableArray array];
    
    for (int i = 0; i < [DataModel object].down_Name_Arr.count; i++) {
        [nameArr addObject:[DataModel object].down_Name_Arr[i][@"tname"]];
    }
    
//    nameArr = @[@"头像",@"昵称",@"性别",@"年龄",@"真实姓名",@"身份码",@"联系方式",@"地址",@"志愿者"];
//    nameArr = [NSArray arrayWithObjects:@"菜单一",@"菜单二",@"菜单三", @"菜单四", @"菜单五", @"菜单一",@"菜单二",@"菜单三", @"菜单四", @"菜单五", nil];

    view1 = [[UIView alloc]init];
    view1.backgroundColor = [UIColor whiteColor];
    view1.layer.cornerRadius = 2.0;
    view1.frame = CGRectMake(15, 10, kWidth/2 - 40, [DataModel object].down_Name_Arr.count * 30);
    [_mBview addSubview:view1];
    
    UIButton *down_Button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    down_Button.frame = CGRectMake(0, 0, kWidth/2 - 40, 16);
    [down_Button setTitle:@"按镇别" forState:UIControlStateNormal];
    down_Button.titleLabel.textAlignment = NSTextAlignmentCenter;
    down_Button.layer.borderWidth = 0.5f;
    down_Button.layer.cornerRadius = 3.0;
    down_Button.titleLabel.font = [UIFont systemFontOfSize:11];
    down_Button.layer.masksToBounds = YES;
    [down_Button addTarget:self action:@selector(down_ButtonAction) forControlEvents:UIControlEventTouchUpInside];
    down_Button.layer.borderColor = [[UIColor colorWithRed:218/255.0 green:218/255.0 blue:219/255.0 alpha:1]CGColor];
    [down_Button setTitleColor:[UIColor colorWithRed:106/255.0 green:107/255.0 blue:108/255.0 alpha:1] forState:UIControlStateNormal];
    [view1 addSubview:down_Button];
    
    UIImageView *down_ImageView = [[UIImageView alloc]initWithFrame:CGRectMake(down_Button.frame.size.width - 13, 4, 10, 8)];
//    down_ImageView.backgroundColor = [UIColor redColor];
    down_ImageView.image = [UIImage imageNamed:@"arrow_img"];
    [down_Button addSubview:down_ImageView];
    
    for (int i = 0; i < [DataModel object].down_Name_Arr.count; i++) {
//        [cityArrays addObject:cityArr[i][@"ciarname"]];
        
//        NSInteger index = i % 4;
        page = i / 4;
        
        // 圆角按钮
        aBt = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        aBt.backgroundColor = [UIColor whiteColor];
        aBt.layer.cornerRadius = 3.0;
        [aBt setTitleColor:[UIColor colorWithRed:106/255.0 green:107/255.0 blue:108/255.0 alpha:1] forState:UIControlStateNormal];
        aBt.layer.masksToBounds = YES;
        aBt.layer.borderWidth = 0.5f;
        aBt.titleLabel.textAlignment = NSTextAlignmentCenter;
        aBt.titleLabel.font = [UIFont systemFontOfSize:13];
        aBt.tag =  i;
        aBt.layer.borderColor = [[UIColor colorWithRed:218/255.0 green:218/255.0 blue:219/255.0 alpha:1]CGColor];
//        aBt.frame = CGRectMake(index * (Button_Width + Width_Space) + Start_X, page  * (Button_Height + Height_Space)+Start_Y, Button_Width, Button_Height);
        aBt.frame = CGRectMake(0, i * 30 + 16, kWidth/2 - 40, 30);
        [aBt addTarget:self action:@selector(aBtAction:) forControlEvents:UIControlEventTouchUpInside];
        [aBt setTitle:[nameArr objectAtIndex:i] forState:UIControlStateNormal];
        [view1 addSubview:aBt];
    }
    
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapClickAction:)];
    [_mBview addGestureRecognizer:singleTap];
}

- (void)aBtAction:(UIButton *)sender {


    UIButton *button = sender;
    [DataModel object].down_name_Str = button.titleLabel.text;
    [DataModel object].down_ID_Str = [DataModel object].down_Name_Arr[button.tag][@"tid"];

    NSLog(@"%@",button.titleLabel.text);
    NSLog(@"%@",[DataModel object].down_ID_Str );

    if (self.diddown_Name_ClickActionBlock) {
        self.diddown_Name_ClickActionBlock();
    }
}

- (void)down_ButtonAction {
    [DataModel object].down_name_Str = @"全部镇";
    [DataModel object].down_ID_Str = @"0";
    if (self.diddown_ButtonActionBlock) {
        self.diddown_ButtonActionBlock();
    }

}

//- (void)animationShow {
//    
//    [UIView animateWithDuration:1 animations:^{
//        
//        [view1 setFrame:CGRectMake(15, 55, kWidth/2 - 40, nameArr.count * 30)];
////        [_down_TableView setFrame:CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)];
//    }];
//}

- (IBAction)singleTapClickAction:(id)sender {
    //    [view removeFromSuperview];
    if (self.didsingleTapClickActionBlock) {
        self.didsingleTapClickActionBlock();
    }
}

- (void)animationHidden {
    
    
}

- (UIColor *) colorWithHexString: (NSString *)color withAlpha:(double)alpha {
    
    NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    
    // String should be 6 or 8 characters
    if ([cString length] < 6) {
        return [UIColor clearColor];
    }
    
    // strip 0X if it appears
    if ([cString hasPrefix:@"0X"])
        cString = [cString substringFromIndex:2];
    if ([cString hasPrefix:@"#"])
        cString = [cString substringFromIndex:1];
    if ([cString length] != 6)
        return [UIColor clearColor];
    
    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    
    //r
    NSString *rString = [cString substringWithRange:range];
    
    //g
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    
    //b
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    
    return [UIColor colorWithRed:((float) r / 255.0f) green:((float) g / 255.0f) blue:((float) b / 255.0f) alpha:alpha];
}


@end
